const point = require('../models/point')
const question = require('../models/question')
const homework = require('../models/homework')
const workRecords = require('../models/workRecords')
const collection = require('../models/collection')

var express = require('express')
var router = express.Router()

var formidable = require('formidable')

router.post('/', (req, res) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields) {
        if(err) {
            res.json({'state': false, 'msg': err})
        }else {
            var find_point = (point_id) => {
                return new Promise(function (resolve, reject) {
                    point.findOne({
                        _id: point_id
                    }, (err, doc) => {
                        if(err) {
                            res.json({'state': false, 'msg': err})
                        }else {
                            resolve(doc);
                        }
                    })
                })
            }
    
            var find_question = (point_id, question_id) => {
                return new Promise(function (resolve, reject) {
                    point.findOne({
                        _id: point_id
                    }, (err, p) => {
                        if(err) {
                            res.json({'state': false, 'msg': err})
                        }else{
                            question.findOne({
                                _id: question_id
                            }, (err, doc) => {
                                if(err) {
                                    res.json({'state': false, 'msg': err})
                                }else {
                                    resolve({
                                        point: p,
                                        question: doc
                                    })
                                }
                            })
                        }
                    })
                })
            }

            var is_collection = (question_id, UUID) => {
                return new Promise(function(resolve, reject) {
                    collection.find({
                        question_ID: question_id,
                        UUID: UUID
                    }, (err, c) => {
                        if(err){
                            res.json({'state': fail, 'msg': err})
                        }else{
                            if(c.length!==0){
                                resolve(true);
                            }else{
                                resolve(false);
                            }
                        }
                    })
                })
            }

            homework.findOne({
                seq: fields.seq
            }, async (err, doc) => {
                if(err) {
                    res.json({'state':false, 'msg': err})
                }else {
                    if(doc){
                        if(doc.type === 'timu'){
                            let question_list = [];
                            for(var i=0; i<doc.question.length; i++) {
                                let item = doc.question[i];
                                if(item.questionId) {
                                    let tmpquestion = await find_question(item.pointId,item.questionId).then();
                                    let is_collected = await is_collection(item.questionId, fields.UUID).then();
                                    if(tmpquestion.point && tmpquestion.question){
                                        let tmpquestion1 = {
                                            _id: tmpquestion.question.point_id,
                                            menu: tmpquestion.point.menu,
                                            seq: tmpquestion.point.seq,
                                            title: tmpquestion.point.title,
                                            content: tmpquestion.point.content,
                                            items: tmpquestion.question.items,
                                            question_id: item.questionId,
                                            question: tmpquestion.question.question,
                                            type: tmpquestion.question.type,
                                            bloom: tmpquestion.question.bloom,
                                            attachment: false,
                                            is_collected: is_collected
                                        }
                                        question_list.push(tmpquestion1);
                                    }
                                }else {
                                    let tmppoint = await find_point(item.pointId).then();
                                    let is_collected = await is_collection(item.pointId, fields.UUID).then();
                                    if(tmppoint){
                                        let tmppoint1 = {
                                            _id: item.pointId,
                                            menu: tmppoint.menu,
                                            seq: tmppoint.seq,
                                            title: tmppoint.title,
                                            content: tmppoint.content,
                                            items: tmppoint.practice.items,
                                            question_id: "",
                                            question: tmppoint.practice.question_editor_Content,
                                            type: tmppoint.practice.type,
                                            bloom: tmppoint.bloom,
                                            attachment: true,
                                            is_collected: is_collected
                                        }
                                        question_list.push(tmppoint1);
                                    }
                                }
                            }
                            res.json({
                                'state': true,
                                'question_list': question_list
                            })
                        }else if(doc.type === 'kaifang') {
                            workRecords.findOne({
                                seq: fields.seq,
                                UUID: fields.UUID
                            }, (err, record) => {
                                if(err) {
                                    res.json({'state': false, 'msg': err})
                                }else {
                                    let result = {}
                                    if(record){
                                        result = {
                                            name: doc.name,
                                            need: doc.need,
                                            time: doc.time,
                                            is_late : doc.is_late,
                                            is_submit : true,
                                            answer: record.question_answer[0].answer
                                        }
                                    }else {
                                        result = {
                                            name: doc.name,
                                            need: doc.need,
                                            time: doc.time,
                                            is_late: doc.is_late,
                                            is_submit: false,
                                            answer: ""
                                        }
                                    }
                                    res.json({
                                        'state': true,
                                        'result': result
                                    })
                                }
                            })
                        }
                    }else{
                        res.json({'state':false, 'msg': '未找到相应作业！'})
                    }
                }
            })
        }
    });
});

module.exports = router;